MineControl = {}
local self = MineControl
Control:Register(self)

function self:Init()
end

function self:SetDiff(idx)
    MineModel:SetDiff(idx)
    EventListener:Broadcast(MineEvent.MINE_DIFF_UP)
end

function self:MineStart()
    MineModel.game_status = MineConstant.GameStatus.Start
    local diff_args = MineConstant.DiffArgs[MineModel.diff_lev]
    local X = diff_args.X
    local Y = diff_args.Y
    local idx = 0
    local grid = nil
    for y = 1, Y do
        for x = 1, X do
            idx = x + (y - 1) * X
            grid = MineModel:GetGrid(x, y)
            if grid == nil then
                grid = MineModel:MakeGrid(idx, x, y)
                MineModel:AddGrid(grid)
            end
        end
    end

    --还原默认参数
    MineModel:ForeachGrid(
        function(grid)
            grid.bomb_count = 0
            grid.is_bomb = false
            grid.is_dig = false
        end
    )
    
    MineModel.bomb_count = 0
    --埋炸弹
    while (not MineModel:IsBombFull()) do
        MineModel:AddBomb()
    end

    --计算雷块附近炸弹
    MineModel:ForeachGrid(
        function(grid)
            local rx = grid.x
            local ry = grid.y
            local num = 0
            if (MineModel:IsBomb(rx - 1, ry - 1)) then
                num = num + 1
            end
            if (MineModel:IsBomb(rx - 1, ry)) then
                num = num + 1
            end
            if (MineModel:IsBomb(rx - 1, ry + 1)) then
                num = num + 1
            end
            if (MineModel:IsBomb(rx, ry - 1)) then
                num = num + 1
            end
            if (MineModel:IsBomb(rx, ry)) then
                num = num + 1
            end
            if (MineModel:IsBomb(rx, ry + 1)) then
                num = num + 1
            end
            if (MineModel:IsBomb(rx + 1, ry - 1)) then
                num = num + 1
            end
            if (MineModel:IsBomb(rx + 1, ry)) then
                num = num + 1
            end
            if (MineModel:IsBomb(rx + 1, ry + 1)) then
                num = num + 1
            end
            if (not grid.is_bomb) then
                grid.bomb_count = num
            end
        end
    )
    EventListener:Broadcast(MineEvent.MINE_UP)
end

function self:Dig(x, y)
    if MineModel.game_status == MineConstant.GameStatus.GameOver then
        return
    end
    local grig = MineModel:GetGrid(x, y)
    if grig then
        --空地
        if grig.bomb_count == 0 then 
            self:DigEmpty(x,y)
        end

        grig.is_dig = true
        if grig.is_bomb then
            MineModel.game_status = MineConstant.GameStatus.GameOver
        end
       
        EventListener:Broadcast(MineEvent.MINE_UP)
    end
end


function self:DigEmpty(x, y)
    local grig = MineModel:GetGrid(x, y)
    if grig then
        --空地
        if grig.is_dig == false and grig.bomb_count == 0 then
            grig.is_dig = true

            self:DigEmpty(x - 1, y - 1)
            self:DigEmpty(x - 1, y)
            self:DigEmpty(x - 1, y + 1)

            self:DigEmpty(x, y - 1)
            self:DigEmpty(x, y + 1)

            self:DigEmpty(x + 1, y - 1)
            self:DigEmpty(x + 1, y)
            self:DigEmpty(x + 1, y + 1)
        else
            return
        end
    end
end

